<script setup>
import { ElMessage } from "element-plus";
import Layer from "@/components/layer/index.vue";
import { defineProps, ref, defineEmits } from "vue";
import { addAPI, updateAPI } from "@/api/wedding/WxPay";

// 使用defineEmits来接收来自父组件的方法
const emit = defineEmits(["getTableData"]);
// 声明props
const props = defineProps({
  layer: {
    type: Object,
    default: () => {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});

/************************************定义变量************************************************/
const ruleForm = ref(null);
const layerDom = ref(null);
const rules = {
  orderCode: [{ required: true, message: "请输入订单编号", trigger: "blur" }],
  payStatus: [{ required: true, message: "请输入支付状态", trigger: "blur" }],
  type: [{ required: true, message: "请输入请求类型", trigger: "blur" }],
  resData: [{ required: true, message: "请输入请求参数", trigger: "blur" }],
  repData: [{ required: true, message: "请输入返回值", trigger: "blur" }],
  creatorName: [{ required: true, message: "请输入创建人姓名", trigger: "blur" }],
  creator: [{ required: true, message: "请输入创建人", trigger: "blur" }],
};
let form = ref({
  id: null,
  orderCode: null,
  payStatus: null,
  type: null,
  resData: null,
  repData: null,
  creatorName: null,
  creator: null,
  createTime: null,
  updateTime: null,
  delFlag: null,
});

/************************************数据回显************************************************/
if (props.layer.row) {
  if (props.layer.row) {
    let source = props.layer.row;
    Object.keys(form.value).forEach((k) => k in source && (form.value[k] = source[k]));
  }
}

/************************************定义事件************************************************/
//提交按钮事件
let submit = () => {
  if (ruleForm) {
    ruleForm.value.validate((valid) => {
      if (valid) {
        let params = form.value;
        if (props.layer.row) {
          updateForm(params);
        } else {
          addForm(params);
        }
      } else {
        return false;
      }
    });
  }
};

let updateForm = (params) => {
  updateAPI(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    emit("getTableData", false);
    layerDom && layerDom.value.close();
  });
};

let addForm = (params) => {
  addAPI(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    emit("getTableData", true);
    layerDom && layerDom.value.close();
  });
};
</script>

<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form
      :model="form"
      :rules="rules"
      ref="ruleForm"
      label-width="120px"
      style="margin-right: 30px"
    >
      <el-form-item label="订单编号：" prop="orderCode">
        <el-input v-model="form.orderCode" placeholder="请输入订单编号" disabled></el-input>
      </el-form-item>
      <el-form-item label="支付状态：" prop="payStatus">
        <el-input v-model="form.payStatus" placeholder="请输入支付状态" disabled></el-input>
      </el-form-item>
      <el-form-item label="请求类型：" prop="type">
        <el-input v-model="form.type" placeholder="请输入请求类型" disabled></el-input>
      </el-form-item>
      <el-form-item label="请求参数：" prop="resData">
        <el-input type="textarea" rows="12" v-model="form.resData" placeholder="请输入请求参数" disabled></el-input>
      </el-form-item>
      <el-form-item label="返回值：" prop="repData">
        <el-input type="textarea" rows="12" v-model="form.repData" placeholder="请输入返回值" disabled></el-input>
      </el-form-item>
      <el-form-item label="创建人姓名：" prop="creatorName">
        <el-input v-model="form.creatorName" placeholder="请输入创建人姓名" disabled></el-input>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<style scoped lang="scss">
.el-input-number--large {
  line-height: 38px;
  width: 230px;
}
</style>
